package com.sinodata.bsm.cicp.collector.was6;

import com.sinodata.bsm.cicp.finder.was6.Util;
import com.sinodata.bsm.cicp.util.CollectorUtil;

public class WasCollectTask {

    private boolean isActive = false;
    private String result;
    private long collectTime = 0L;
    private String ip;
    private String port;
    private String module;

    public WasCollectTask(String _ip, String _port, String _module) {
        this.ip = _ip;
        this.port = _port;
        this.module = _module;
    }

    public long getCollectTime() {
        return collectTime;
    }

    public void setCollectTime(long collectTime) {
        this.collectTime = collectTime;
    }

    public boolean isActive() {
        return isActive;
    }

    public synchronized String getXml() {
        if (System.currentTimeMillis() - collectTime < 22 * 60 * 1000L) {
            return this.result;
        }
        collect();
        return this.result;
    }

    private void collect() {
        this.isActive = true;
        String url = Util.getWasPerfToolUrl(ip, port);
        String urlStr = url + "?Module=" + this.module;
        try {
            String respXml = CollectorUtil.getJmxXml(urlStr, ip, Integer.parseInt(port));
            this.result = respXml;
        } catch (Exception e) {
        } finally {
            this.collectTime = System.currentTimeMillis();
            this.isActive = false;
        }
    }

    public String getResult() {
        return result;
    }

}
